#Current Date - 02/05/2024
import pandas as pd
import yfinance as yf
import plotly.express as px
from datetime import datetime
start_date = datetime.now() - pd.DateOffset(months=12)
end_date = datetime.now()
tickers = ['AAPL', 'SPY']
df_list = []
for ticker in tickers:
data = yf.download(ticker, start=start_date, end=end_date)
df_list.append(data)
df = pd.concat(df_list, keys=tickers, names=['Ticker', 'Date'])
df = df.reset_index() # we have to reset index
print(df.head(10))
[*********************100%%**********************] 1 of 1 completed [*********************100%%**********************] 1 of 1 completed
Ticker Date Open High Low Close \
0 AAPL 2023-02-06 152.570007 153.100006 150.779999 151.729996
1 AAPL 2023-02-07 150.639999 155.229996 150.639999 154.649994
2 AAPL 2023-02-08 153.880005 154.580002 151.169998 151.919998
3 AAPL 2023-02-09 153.779999 154.330002 150.419998 150.869995
4 AAPL 2023-02-10 149.460007 151.339996 149.220001 151.009995
5 AAPL 2023-02-13 150.949997 154.259995 150.919998 153.850006
6 AAPL 2023-02-14 152.119995 153.770004 150.860001 153.199997
7 AAPL 2023-02-15 153.110001 155.500000 152.880005 155.330002
8 AAPL 2023-02-16 153.509995 156.330002 153.350006 153.710007
9 AAPL 2023-02-17 152.350006 153.000000 150.850006 152.550003
Adj Close Volume
0 150.886597 69858300
1 153.790390 83322600
2 151.075546 64120100
3 150.031387 56007100
4 150.399887 57450700
5 153.228439 62199000
6 152.581055 61707600
7 154.702454 65573800
8 153.088989 68167900
9 151.933685 59144100
fig = px.line(df, x= 'Date',
y='Close',
color='Ticker',
title="Stock Market Performance for the Past Year")
fig.show()
fig = px.area(df, x='Date', y='Close',
facet_col='Ticker',
labels={'Date':'Date', 'Close':'Closing Price', 'Ticker':'Company'},
title='Stock Prices for Apple compared to the S&P 500')
fig.show()
df['MA10'] = df.groupby('Ticker')['Close'].rolling(window=10).mean().reset_index(0, drop=True)
df['MA20'] = df.groupby('Ticker')['Close'].rolling(window=30).mean().reset_index(0, drop=True)
df['Volatility'] = df.groupby('Ticker')['Close'].pct_change().rolling(window=10).std().reset_index(0, drop=True)
fig = px.line(df, x='Date', y='Volatility',
color='Ticker',
title='Volatility between AAPL and SPY Tickers')
fig.show()
#Worth noting that AAPL is in the S&P 500 Index Fund (and Stocks in the S&P are not equally weighted, so AAPL
#does have a big impact) but still worth reviewing as other companies are a part of that Fund.
#Helps show how the health of the market partially impacts a company's stock.
# create a DataFrame with the stock prices of Apple and SPY
apple = df.loc[df['Ticker'] == 'AAPL', ['Date', 'Close']].rename(columns={'Close': 'AAPL'})
google = df.loc[df['Ticker'] == 'SPY', ['Date', 'Close']].rename(columns={'Close': 'SPY'})
df_corr = pd.merge(apple, google, on='Date')
# create a scatter plot to visualize the correlation
fig = px.scatter(df_corr, x='AAPL', y='SPY',
trendline='ols',
title='Correlation between Apple and S&P 500')
fig.show()
#Now to focus on Apple to see what a good entry point may be by determining max/min for year
df_list = []
data = yf.download("AAPL", start=start_date, end=end_date)
df_list.append(data)
df = pd.concat(df_list, keys=tickers, names=['Ticker', 'Date'])
df = df.reset_index()
[*********************100%%**********************] 1 of 1 completed
print("Past Year")
print("Highest Value")
print(df.max())
print("Lowest Value")
print(df.min())
Past Year Highest Value Ticker AAPL Date 2024-02-02 00:00:00 Open 198.020004 High 199.619995 Low 197.0 Close 198.110001 Adj Close 198.110001 Volume 128256700 dtype: object Lowest Value Ticker AAPL Date 2023-02-06 00:00:00 Open 144.380005 High 146.710007 Low 143.899994 Close 145.309998 Adj Close 144.722946 Volume 24048300 dtype: object
#Narrowing down to this past half month for a realistic call option strike price to purchase
start_date = datetime.now() - pd.DateOffset(weeks=2)
end_date = datetime.now()
df_list = []
data = yf.download("AAPL", start=start_date, end=end_date)
df_list.append(data)
df = pd.concat(df_list, keys=tickers, names=['Ticker', 'Date'])
df = df.reset_index()
print("Past Month")
print("Highest Value")
print(df.max())
print("Lowest Value")
print(df.min())
[*********************100%%**********************] 1 of 1 completed
Past Month Highest Value Ticker AAPL Date 2024-02-02 00:00:00 Open 195.419998 High 196.380005 Low 194.339996 Close 195.179993 Adj Close 195.179993 Volume 102518000 dtype: object Lowest Value Ticker AAPL Date 2024-01-22 00:00:00 Open 179.860001 High 186.949997 Low 179.25 Close 184.399994 Adj Close 184.399994 Volume 42355600 dtype: object
#Given the gradual increase, a solid strike price may be $195-$199 based on expiration date
#Based on the data above, while short term holdings may not be encouraged based on
#market volatility, both SPY and AAPL specifically would be great holds long term